//此为格式转换函数


const uchar ASCTable[] ="0123456789ABCDEF";

//将一个ASCII码转化为十六进制,如果失败返回-1
int Ascii_Hex(char asc)
{
    if(asc >= '0' && asc <= '9')
        return (asc-'0');
    else if(asc >= 'A' &&asc <= 'F')
        return (0x0a+asc-'A');
    else
        return -1;
}
//将两个ASCII码转化为十六进制.如果失败返回-1
int Ascii2_Hex(uchar *asc)
{
    uchar i,temp[2];

    memcpy(temp,asc,2);
    for(i=0; i<2; i++)
    {
        if(Ascii_Hex(temp[i])==-1)
            return -1;
        else
            temp[i]=Ascii_Hex(temp[i]);
    }
    return (temp[0]*0x10+temp[1]);
}
//将hex转换为ascii码.如果失败返回-1
int Hex_Ascii(uchar ds)
{
    if(ds<=15)
        return(ASCTable[ds]);
    else
        return -1;
}
//1Byte十六进制数转换为2个ASC码表示，ex: 0x1A ---->'1''A'
void Hex_Ascii2(uchar hex,uchar *asc)
{
    asc[0]=Hex_Ascii(hex>>4);
    asc[1]=Hex_Ascii(hex&0x0F);
}
//1Byte的HEX按十进制数转换为2个ASC码表示，ex: 0x1A ---->'2''6'，0x6F ---->'6''F'
void Dec_Ascii2(uchar dec,uchar *asc)
{
    if(dec<100)
    {
        asc[0]=Hex_Ascii(dec/10);
        asc[1]=Hex_Ascii(dec%10);
    }
    else
    {
        asc[0]=Hex_Ascii(dec>>4);
        asc[1]=Hex_Ascii(dec&0x0F);
    }
}
//1Byte的十六进制表示的十进制转换为十六进制，ex: 0x11  --->  0x0B
uchar Dech_Hex(uchar dat)
{
    if(((dat&0xf0) > 0x90) || ((dat&0x0f) > 0x09))
        return 0xff;
    return (((dat>>4)&0x0f)*10 + (dat&0x0f));
}
//十进制数的ASC码转换为2个字节的十六进制数， ex:  '1' '0' '0' '0'---> 0x3E8	失败返回-1
uchar DecNum_Hex(uchar *asc, uchar len, uint *ret)
{
    uchar i;
    uint tmp;

    tmp = 1;
    for(i=len; i>0; i--)
    {
        if((asc[i-1] < '0') || (asc[i-1] > '9'))
            return 0;
        *ret += tmp*(asc[i-1]-'0');
        tmp *= 10;
    }
    return 1;
}
/*
//4个表示十进制数的ASC码转换为2个字节的十六进制数， ex:  '1' '0' '0' '0'---> 0x3E8	失败返回-1
int Dec4_Hex(uchar *asc)
{
	uchar i;
	for(i=0; i<4; i++)
	{
		if((asc[i]<'0') && (asc[i] > '9'))
			return -1;
	}
	return ((asc[0]-'0')*1000+(asc[1]-'0')*100+(asc[2]-'0')*10+(asc[3]-'0'));
}*/
//2个表示十进制数的ASC码转换为2个字节的十六进制数， ex:  '1' '0' ---> 0x0A	失败返回-1
int Dec2_Hex(uchar *asc)
{
    uchar i;
    for(i=0; i<2; i++)
    {
        if((asc[i]<'0') && (asc[i] > '9'))
            return -1;
    }
    return ((asc[0]-'0')*10+(asc[1]-'0'));
}
//1Byte的十六进制数转换为2个ASC码表示的十进制数，只用于日期的转换，ex: 0x0B ---> '1' '1'
void Hex_Dec2(uchar hex,uchar *asc)
{
    uchar temp;
    if(hex<100)
    {
        asc[0]=(hex/10) + '0';
        asc[1]=(hex%10) + '0';
    }
    else
    {
        temp=hex - 100;
        asc[0]=(temp/10) + '0';
        asc[1]=(temp%10) + '0';
    }
}
/*
//十六进制 转换成 十进制，ex: 0x50 ---> 0x0080，0x64 ---> 0x0100
uint Hex_DocInt(uchar dec)
{
	uint h;

	h=(dec/100)<<8+((dec%100)/10)<<4+(dec%10);
	return h;
}*/
//十六进制 转换成 十进制，ex: 0x50 ---> 0x80
uchar Hex_Doc2(uchar hex)
{
    uchar doc;

    if(hex > 99)
    {
        return 0xff;
    }
    else
    {
        doc = (hex/10)<<4;
        doc += hex%10;
        return doc;
    }
}
#if (TESTFunction > 0)
//十六进制 转换成 十进制，ex: 0x000F423F ---> 0x999999
void Hex_Doc3(u32 hex, uchar *asc)
{
    u32 tmp = hex%1000000;

    asc[0] = (tmp/100000)<<4;
    tmp = tmp%100000;
    asc[0] += tmp/10000;
    tmp = tmp%10000;
    asc[1] = (tmp/1000)<<4;
    tmp = tmp%1000;
    asc[1] += tmp/100;
    tmp = tmp%100;
    asc[2] = (tmp/10)<<4;
    asc[2] += (tmp%10);
}
#endif
/*
//十六进制 转换成 十进制ASCII，ex: 0x50 ---> '8' '0'
void Hex_Doc2ASC(uchar hex, uchar *asc)
{
	if(hex > 99)
	{
		asc[0] = 'Y';
		asc[1] = 'Y';
	}
	else
	{
		asc[0] = hex/10 + '0';
		asc[1] = hex%10 + '0';
	}
}*/
//反向D0~7转为D7~0
uchar DataChunge(uchar data)
{
    uchar i, tmp1, tmp2;

    tmp1 = data;
    tmp2 = 0;

    for(i=0; i<8; i++)
    {
        tmp2 >>= 1;
        if(tmp1>0x7F)
            tmp2 |= 0x80;
        tmp1 <<= 1;
    }

    return tmp2;
}

//crc16校验程序
uint getCRC16(uchar *pData, uchar DataLen)
{
    uint i=0, acc=0;

    while(DataLen--)
    {
        i = (uchar)(*pData);
        pData++;
        acc ^= (i<<8);
        for(i=0; i<8; i++)
        {
            if(acc&0x8000)
            {
                acc = (acc<<1)^0x1021;
            }
            else
            {
                acc <<= 1;
            }
        }
    }
    i = acc & 0xffff;
    return  (uint)i;
}

void forceSoftReset(void)
{
    //while(1);
    NVIC_SystemReset();
}



//16进制转化为
unsigned char HexToChar(unsigned char bChar)
{
    if((bChar>=0x30)&&(bChar<=0x39))
    {
        bChar -= 0x30;
    }
    else if((bChar>=0x41)&&(bChar<=0x46)) // Capital
    {
        bChar -= 0x37;
    }
    else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase
    {
        bChar -= 0x57;
    }
    else
    {
        bChar = 0xff;
    }
    return bChar;
}

